﻿#include "SEAAreaLeakDialog.h"
#include "ui_SEAAreaLeakDialog.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAJunctionArea.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsShellPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"

Q_DECLARE_METATYPE(Acoustics::FITKSEAJunAreaLeakStr::LeakType)

namespace GUI
{
    SEAAreaLeakDialog::SEAAreaLeakDialog(Acoustics::FITKSEAJunAreaLeakStr leaks, QWidget * parent) :
        GUIDialogBase(parent), _leakInfo(leaks)
    {
        //更改状态
        setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose, false);
        _ui = new Ui::SEAAreaLeakDialog();
        _ui->setupUi(this);

        //初始化渗透类型
        _ui->comboBox_LeakType->addItem("Rectangular", QVariant::fromValue(Acoustics::FITKSEAJunAreaLeakStr::LeakType::Rectangular));
        _ui->comboBox_LeakType->addItem("Circular", QVariant::fromValue(Acoustics::FITKSEAJunAreaLeakStr::LeakType::Circular));
        _ui->comboBox_LeakType->addItem("Slit", QVariant::fromValue(Acoustics::FITKSEAJunAreaLeakStr::LeakType::Slit));
        _ui->comboBox_LeakType->addItem("UserDefined", QVariant::fromValue(Acoustics::FITKSEAJunAreaLeakStr::LeakType::UserDefined));
        _ui->comboBox_LeakType->setCurrentIndex(0);

        //初始化TL
        if (_physics == nullptr)return;
        Acoustics::FITKAcousticsSpectraManager* spectraMan = _physics->getFITKAcousticsSpectraManager();
        if (spectraMan) {
            for (auto spectra : spectraMan->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::TransmissionLoss)) {
                if (spectra == nullptr)continue;
                _ui->comboBox_TLSpec->addItem(spectra->getDataObjectName(), spectra->getDataObjectID());
            }
            if (_ui->comboBox_TLSpec->count() > 0)_ui->comboBox_TLSpec->setCurrentIndex(0);
        }

        setDataToWidget(_leakInfo);
    }

    SEAAreaLeakDialog::~SEAAreaLeakDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    Acoustics::FITKSEAJunAreaLeakStr SEAAreaLeakDialog::getData()
    {
        return _leakInfo;
    }

    void SEAAreaLeakDialog::on_pushButton_Ok_clicked()
    {
        _leakInfo = getDataFromWidget();
        this->accept();
    }

    void SEAAreaLeakDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void GUI::SEAAreaLeakDialog::on_comboBox_LeakType_currentIndexChanged(int index)
    {
        Q_UNUSED(index);
        
        _ui->lineEdit_Length->setEnabled(true);
        _ui->lineEdit_Width->setEnabled(true);
        _ui->lineEdit_Radius->setEnabled(true);
        _ui->lineEdit_Depth->setEnabled(true);
        _ui->lineEdit_Area->setEnabled(true);
        _ui->comboBox_TLSpec->setEnabled(true);

        auto type = _ui->comboBox_LeakType->currentData().value<Acoustics::FITKSEAJunAreaLeakStr::LeakType>();
        switch (type){
        case Acoustics::FITKSEAJunAreaLeakStr::Rectangular:{
            _ui->lineEdit_Radius->setEnabled(false);
            _ui->lineEdit_Area->setEnabled(false);
            _ui->comboBox_TLSpec->setEnabled(false);
            break;
        }
        case Acoustics::FITKSEAJunAreaLeakStr::Circular: {
            _ui->lineEdit_Length->setEnabled(false);
            _ui->lineEdit_Width->setEnabled(false);
            _ui->lineEdit_Area->setEnabled(false);
            _ui->comboBox_TLSpec->setEnabled(false);
            break;
        }
        case Acoustics::FITKSEAJunAreaLeakStr::Slit: {
            _ui->lineEdit_Radius->setEnabled(false);
            _ui->lineEdit_Area->setEnabled(false);
            _ui->comboBox_TLSpec->setEnabled(false);
            break;
        }
        case Acoustics::FITKSEAJunAreaLeakStr::UserDefined: {
            _ui->lineEdit_Length->setEnabled(false);
            _ui->lineEdit_Width->setEnabled(false);
            _ui->lineEdit_Radius->setEnabled(false);
            _ui->lineEdit_Depth->setEnabled(false);
            break;
        }
        }
    }

    void SEAAreaLeakDialog::setDataToWidget(Acoustics::FITKSEAJunAreaLeakStr leakinfo)
    {
        _ui->comboBox_LeakType->setCurrentIndex(_ui->comboBox_LeakType->findData(leakinfo._type));
        _ui->lineEdit_Name->setText(leakinfo._name);
        _ui->lineEdit_Length->setText(QString::number(leakinfo._length));
        _ui->lineEdit_Length->setEnabled(leakinfo._isLength);
        _ui->lineEdit_Width->setText(QString::number(leakinfo._width));
        _ui->lineEdit_Width->setEnabled(leakinfo._isWidth);
        _ui->lineEdit_Radius->setText(QString::number(leakinfo._radius));
        _ui->lineEdit_Radius->setEnabled(leakinfo._isRadius);
        _ui->lineEdit_Depth->setText(QString::number(leakinfo._depth));
        _ui->lineEdit_Depth->setEnabled(leakinfo._isDepth);
        _ui->lineEdit_Area->setText(QString::number(leakinfo._area));
        _ui->lineEdit_Area->setEnabled(leakinfo._isArea);
        _ui->comboBox_TLSpec->setCurrentIndex(_ui->comboBox_TLSpec->findData(leakinfo._TLSpectrumID));
        _ui->comboBox_TLSpec->setEnabled(leakinfo._isTLSpectrumID);
        _ui->checkBox_EnabledJunction->setChecked(leakinfo._isEnabled);
    }

    Acoustics::FITKSEAJunAreaLeakStr SEAAreaLeakDialog::getDataFromWidget()
    {
        Acoustics::FITKSEAJunAreaLeakStr leakinfo;
        leakinfo._type = _ui->comboBox_LeakType->currentData().value<Acoustics::FITKSEAJunAreaLeakStr::LeakType>();
        leakinfo._name = _ui->lineEdit_Name->text();
        leakinfo._length = _ui->lineEdit_Length->text().toDouble();
        leakinfo._isLength = _ui->lineEdit_Length->isEnabled();
        leakinfo._width = _ui->lineEdit_Width->text().toDouble();
        leakinfo._isWidth = _ui->lineEdit_Width->isEnabled();
        leakinfo._radius = _ui->lineEdit_Radius->text().toDouble();
        leakinfo._isRadius = _ui->lineEdit_Radius->isEnabled();
        leakinfo._depth = _ui->lineEdit_Depth->text().toDouble();
        leakinfo._isDepth = _ui->lineEdit_Depth->isEnabled();
        leakinfo._area = _ui->lineEdit_Area->text().toDouble();
        leakinfo._isArea = _ui->lineEdit_Area->isEnabled();
        leakinfo._TLSpectrumID = _ui->comboBox_TLSpec->currentData().toInt();
        leakinfo._isTLSpectrumID = _ui->comboBox_TLSpec->isEnabled();
        leakinfo._isEnabled = _ui->checkBox_EnabledJunction->isChecked();

        if (leakinfo._isArea == false) {
            leakinfo.calculateArea();
        }

        return leakinfo;
    }
}
